﻿using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using XUtliPoolLib;

namespace XMainClient
{
    // Token: 0x02000E00 RID: 3584
    internal class XTutorialCmdParser
    {
        // Token: 0x0600C177 RID: 49527 RVA: 0x00294E1C File Offset: 0x0029301C
        public bool Parse(string file, int initStep, ref Queue<XTutorialCmd> cmdQueue, int execStep)
        {
            Stream stream = XSingleton<XResourceLoaderMgr>.singleton.ReadText(file, ".txt", true);
            StreamReader streamReader = new StreamReader(stream);
            string text = streamReader.ReadLine();
            int num = 1;
            int num2 = 0;
            XTutorialCmd xtutorialCmd = null;
            bool flag = execStep <= 0;
            for (; ; )
            {
                text = streamReader.ReadLine();
                num++;
                if (text.StartsWith("end tutorial"))
                {
                    break;
                }
                if (text.Length != 0)
                {
                    if (!text.StartsWith("--"))
                    {
                        string[] array = text.Split(XGlobalConfig.TabSeparator, StringSplitOptions.RemoveEmptyEntries);
                        bool flag5 = false;
                        if (text.StartsWith("step"))
                        {
                            num2++;
                            if (num2 <= initStep)
                            {
                                continue;
                            }
                            this._parseTmpCmd = new XTutorialCmd();
                            this._parseTmpCmd.mainTutorialBit = execStep;
                            if (flag)
                            {
                                this._parseTmpCmd.step = num2;
                            }
                            else
                            {
                                this._parseTmpCmd.step = -1;
                            }
                            if (array.Length > 1)
                            {
                                this._parseTmpCmd.tag = array[1];
                            }
                            else
                            {
                                this._parseTmpCmd.tag = "NULL";
                            }
                            this._parseTmpCmd.conditions = new List<XTutorialCmdExecuteCondition>();
                            this._parseTmpCmd.condParams = new List<string>();
                            this._parseTmpCmd.endParam = new List<string>();
                            this._parseTmpCmd.endcondition = XTutorialCmdFinishCondition.No_Condition;
                            this._parseTmpCmd.state = XCmdState.Cmd_In_Queue;
                            this._parseTmpCmd.bLastCmdInQueue = false;
                            this._parseTmpCmd.isOutError = true;
                            this._parseTmpCmd.TutorialID = execStep;
                            this._parseTmpCmd.isCanDestroyOverlay = true;
                            flag5 = true;
                        }
                        else if (text.StartsWith("scond"))
                        {
                            if (this._parseTmpCmd != null)
                            {
                                if (array.Length > 1)
                                {
                                    this._parseTmpCmd.conditions.Add(this.Str2Condition(array[1]));
                                }
                                if (array.Length > 2)
                                {
                                    this._parseTmpCmd.condParams.Add(array[2]);
                                }
                                else
                                {
                                    this._parseTmpCmd.condParams.Add("null");
                                }
                            }
                            flag5 = true;
                        }
                        else if (text.StartsWith("econd"))
                        {
                                
                            if (this._parseTmpCmd != null)
                            {
                                if (array.Length > 1)
                                {
                                    this._parseTmpCmd.endcondition = this.Str2EndCondition(array[1]);
                                }
                                if (array.Length > 2)
                                {
                                    this._parseTmpCmd.endParam.Add(array[2]);
                                }
                            }
                            flag5 = true;
                        }
                        else if (text.StartsWith("text"))
                        {
                            if (this._parseTmpCmd != null)
                            {
                                if (array.Length > 1)
                                {
                                    this._parseTmpCmd.text = array[1];
                                }
                                if (array.Length > 3)
                                {
                                    this._parseTmpCmd.textPos = new Vector3((float)int.Parse(array[2]), (float)int.Parse(array[3]));
                                }
                                else
                                {
                                    if (array.Length > 2)
                                    {
                                        this._parseTmpCmd.textPos = new Vector3((float)int.Parse(array[2]), 0f);
                                    }
                                }
                            }
                            flag5 = true;
                        }
                        else if (text.StartsWith("internaldelay"))
                        {
                            if (this._parseTmpCmd != null)
                            {
                                if (array.Length > 1)
                                {
                                    this._parseTmpCmd.interalDelay = float.Parse(array[1]);
                                }
                            }
                            flag5 = true;
                        }
                        else if (text.StartsWith("ailin"))
                        {
                            if (this._parseTmpCmd != null)
                            {
                                if (array.Length > 1)
                                {
                                    this._parseTmpCmd.ailinText = array[1];
                                }
                                if (array.Length > 2)
                                {
                                    this._parseTmpCmd.ailinPos = int.Parse(array[2]);
                                }
                                if (array.Length > 3)
                                {
                                    this._parseTmpCmd.ailinText2 = array[3];
                                }
                            }
                            flag5 = true;
                        }
                        else if (text.StartsWith("buttomtext"))
                        {
                                                
                            if (this._parseTmpCmd != null)
                            {
                                if (array.Length > 1)
                                {
                                    this._parseTmpCmd.buttomtext = array[1];
                                }
                            }
                            flag5 = true;
                        }
                        else if (text.StartsWith("pause"))
                        {
                            if (this._parseTmpCmd != null)
                            {
                                this._parseTmpCmd.pause = true;
                            }
                            flag5 = true;
                        }
                        else if (text.StartsWith("audio"))
                        {
                            if (this._parseTmpCmd != null)
                            {
                                if (array.Length > 1)
                                {
                                    this._parseTmpCmd.audio = array[1];
                                }
                            }
                            flag5 = true;
                        }
                        else if (text.StartsWith("skip"))
                        {
                            if (this._parseTmpCmd != null)
                            {
                                if (array.Length > 1)
                                {
                                    this._parseTmpCmd.skipCondition = array[1];
                                }
                                if (array.Length > 2)
                                {
                                    this._parseTmpCmd.skipParam1 = array[2];
                                }
                                if (array.Length > 3)
                                {
                                    this._parseTmpCmd.skipParam2 = array[3];
                                }
                                if (array.Length > 4)
                                {
                                    this._parseTmpCmd.skipParam3 = array[4];
                                }
                            }
                            flag5 = true;
                        }
                        else if (text.StartsWith("scroll"))
                        {
                            if (this._parseTmpCmd != null)
                            {
                                if (array.Length > 1)
                                {
                                    this._parseTmpCmd.scroll = array[1];
                                }
                                if (array.Length > 2)
                                {
                                    this._parseTmpCmd.scrollPos = int.Parse(array[2]);
                                }
                            }
                            flag5 = true;
                        }
                        else if (text.StartsWith("function"))
                        {
                            if (this._parseTmpCmd != null)
                            {
                                if (array.Length > 1)
                                {
                                    this._parseTmpCmd.function = array[1];
                                }
                                if (array.Length > 2)
                                {
                                    this._parseTmpCmd.functionparam1 = array[2];
                                }
                            }
                            flag5 = true;
                        }
                        else if (text.StartsWith("nodestroyoverlay"))
                        {
                            this._parseTmpCmd.isCanDestroyOverlay = false;
                            flag5 = true;
                        }
                        else if (this._parseTmpCmd != null)
                        {
                            for (int i = 0; i < this._validateCmd.Length; i++)
                            {
                                if (array[0] == this._validateCmd[i])
                                {
                                    this._parseTmpCmd.cmd = array[0];
                                    if (array.Length > 1)
                                    {
                                        this._parseTmpCmd.param1 = array[1];
                                    }
                                    if (array.Length > 2)
                                    {
                                        this._parseTmpCmd.param2 = array[2];
                                    }
                                    if (array.Length > 3)
                                    {
                                        this._parseTmpCmd.param3 = array[3];
                                    }
                                    if (array.Length > 4)
                                    {
                                        this._parseTmpCmd.param4 = array[4];
                                    }
                                    if (array.Length > 5)
                                    {
                                        this._parseTmpCmd.param5 = array[5];
                                    }
                                    if (array.Length > 6)
                                    {
                                        this._parseTmpCmd.param6 = array[6];
                                    }
                                    cmdQueue.Enqueue(this._parseTmpCmd);
                                    xtutorialCmd = this._parseTmpCmd;
                                    flag5 = true;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            flag5 = true;
                        }
       
                        if (!flag5)
                        {
                            XSingleton<XDebug>.singleton.AddErrorLog(file, "Tutorial Format Error at line ", num.ToString(), " !", null, null);
                            XSingleton<XResourceLoaderMgr>.singleton.ClearStream(stream);
                            return false;
                        }
                    }
                }
            }
            if (xtutorialCmd != null)
            {
                xtutorialCmd.bLastCmdInQueue = true;
                xtutorialCmd.TutorialID = execStep;
                if (!flag)
                {
                    xtutorialCmd.step = execStep;
                }
            }
            XSingleton<XResourceLoaderMgr>.singleton.ClearStream(stream);
            return true;
        }

        // Token: 0x0600C178 RID: 49528 RVA: 0x00295680 File Offset: 0x00293880
        public bool Parse(string file, ref List<XTutorialMainCmd> cmdList, ref XBetterDictionary<uint, XTutorialMainCmd> _SysTutorial)
        {
            XTutorialMainCmd xtutorialMainCmd = new XTutorialMainCmd();
            Stream stream = XSingleton<XResourceLoaderMgr>.singleton.ReadText(file, ".txt", true);
            StreamReader streamReader = new StreamReader(stream);
            string text = streamReader.ReadLine();
            int num = 1;
            for (; ; )
            {
                text = streamReader.ReadLine();
                num++;
                if (text.StartsWith("end tutorial"))
                {
                    break;
                }
                if (text.Length != 0)
                {
                    if (!text.StartsWith("--"))
                    {
                        string[] array = text.Split(XGlobalConfig.TabSeparator, StringSplitOptions.RemoveEmptyEntries);
                        bool flag4 = false;
                        if (text.StartsWith("tutorial"))
                        {
                            int savebit = int.Parse(array[1]);
                            xtutorialMainCmd = new XTutorialMainCmd();
                            xtutorialMainCmd.savebit = savebit;
                            if (array.Length > 2)
                            {
                                xtutorialMainCmd.tag = array[2];
                            }
                            else
                            {
                                xtutorialMainCmd.tag = "NULL";
                            }
                            if (array.Length > 3)
                            {
                                xtutorialMainCmd.isMust = (array[3] == "must");
                            }
                            else
                            {
                                xtutorialMainCmd.isMust = false;
                            }
                            xtutorialMainCmd.conditions = new List<XTutorialCmdExecuteCondition>();
                            xtutorialMainCmd.condParams = new List<string>();
                            flag4 = true;
                        }
                        else if (text.StartsWith("scond"))
                        {
                            if (xtutorialMainCmd != null)
                            {
                                if (array.Length > 1)
                                {
                                    XTutorialCmdExecuteCondition xtutorialCmdExecuteCondition = this.Str2Condition(array[1]);
                                    xtutorialMainCmd.conditions.Add(xtutorialCmdExecuteCondition);
                                    if (array.Length > 2)
                                    {
                                        xtutorialMainCmd.condParams.Add(array[2]);
                                        if (xtutorialCmdExecuteCondition == XTutorialCmdExecuteCondition.Sys_Notify)
                                        {
                                            //bool flag13 = array.Length > 3 && int.Parse(array[3]) > 0;
                                            if (/*!flag13*/array.Length <= 3 || int.Parse(array[3]) <= 0)
                                            {
                                                _SysTutorial.Add(uint.Parse(array[2]), xtutorialMainCmd);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        xtutorialMainCmd.condParams.Add("");
                                    }
                                }
                            }
                            flag4 = true;
                        }
                        else if (text.StartsWith("exec"))
                        {
                            if (xtutorialMainCmd != null)
                            {
                                xtutorialMainCmd.subTutorial = array[1];
                                cmdList.Add(xtutorialMainCmd);
                            }
                            flag4 = true;
                        }

                        if (!flag4)
                        {
                            XSingleton<XDebug>.singleton.AddLog(file, "Tutorial Format Error at line ", num.ToString(), " !", null, null, XDebugColor.XDebug_None);
                            XSingleton<XResourceLoaderMgr>.singleton.ClearStream(stream);
                            return false;
                        }
                    }
                }
            }
            XSingleton<XResourceLoaderMgr>.singleton.ClearStream(stream);
            return true;
        }

        // Token: 0x0600C179 RID: 49529 RVA: 0x0029591C File Offset: 0x00293B1C
        private XTutorialCmdExecuteCondition Str2Condition(string strCondition)
        {
            if (strCondition == "playerlevel")
            {
                return XTutorialCmdExecuteCondition.Player_Level;
            }
            else if (strCondition == "afterlevel")
            {
                return XTutorialCmdExecuteCondition.After_Level;
            }
            else if (strCondition == "inlevel")
            {
                return XTutorialCmdExecuteCondition.In_Level;
            }
            else if (strCondition == "musou")
            {
                return XTutorialCmdExecuteCondition.Musou_Above;
            }
            else if (strCondition == "castskill")
            {
                return XTutorialCmdExecuteCondition.Cast_Skill;
            }
            else if (strCondition == "exstring")
            {
                return XTutorialCmdExecuteCondition.External_String;
            }
            else if (strCondition == "TalkingNpc")
            {
                return XTutorialCmdExecuteCondition.Talk_Npc;
            }
            else if (strCondition == "accepttask")
            {
                return XTutorialCmdExecuteCondition.Can_Accept_Task;
            }
            else if (strCondition == "dotaskbattle")
            {
                return XTutorialCmdExecuteCondition.Task_Battle;
            }
            else if (strCondition == "taskscenefinished")
            {
                return XTutorialCmdExecuteCondition.Task_Scene_Finish;
            }
            else if (strCondition == "taskover")
            {
                return XTutorialCmdExecuteCondition.Task_Over;
            }
            else if (strCondition == "finishtask")
            {
                return XTutorialCmdExecuteCondition.Can_Finish_Task;
            }
            else if (strCondition == "delay")
            {
                return XTutorialCmdExecuteCondition.Time_Delay;
            }
            else if (strCondition == "SysNotify")
            {
                return XTutorialCmdExecuteCondition.Sys_Notify;
            }
            else if (strCondition == "meetenemy")
            {
                return XTutorialCmdExecuteCondition.Meet_Enemy;
            }
            else if (strCondition == "hastarget")
            {
                return XTutorialCmdExecuteCondition.Has_Target;
            }
            else if (strCondition == "getfocus")
            {
                return XTutorialCmdExecuteCondition.Get_Focused;
            }
            else if (strCondition == "artskill")
            {
                return XTutorialCmdExecuteCondition.Art_Skill;
            }
            else if (strCondition == "superarmor0")
            {
                return XTutorialCmdExecuteCondition.No_SuperAmor;
            }
            else if (strCondition == "cutsceneover")
            {
                return XTutorialCmdExecuteCondition.Cutscene_Over;
            }
            else if (strCondition == "bossexist")
            {
                return XTutorialCmdExecuteCondition.Boss_Exist;
            }
            else if (strCondition == "enemyonground")
            {
                return XTutorialCmdExecuteCondition.Enemy_OnGround;
            }
            else if (strCondition == "nopromote")
            {
                return XTutorialCmdExecuteCondition.No_Promote;
            }
            else if (strCondition == "team2")
            {
                return XTutorialCmdExecuteCondition.Team2;
            }
            else if (strCondition == "MainUI")
            {
                return XTutorialCmdExecuteCondition.MainUI;
            }
            else if (strCondition == "item")
            {
                return XTutorialCmdExecuteCondition.Has_Item;
            }
            else if (strCondition == "hasbody")
            {
                return XTutorialCmdExecuteCondition.Has_Body;
            }
            else if (strCondition == "nostackui")
            {
                return XTutorialCmdExecuteCondition.No_Stackui;
            }
            else if (strCondition == "activityopen")
            {
                return XTutorialCmdExecuteCondition.Activity_Open;
            }
            else if (strCondition == "dragoncrusadeopen")
            {
                return XTutorialCmdExecuteCondition.Dragon_Crusade_Open;
            }
            else if (strCondition == "battleNPCtalkend")
            {
                return XTutorialCmdExecuteCondition.Battle_NPC_Talk_End;
            }
            else
            {
                return XTutorialCmdExecuteCondition.No_Condition;
            }
        }

        // Token: 0x0600C17A RID: 49530 RVA: 0x00295C18 File Offset: 0x00293E18
        private XTutorialCmdFinishCondition Str2EndCondition(string strCondition)
        {
            if (strCondition == "click")
            {
                return XTutorialCmdFinishCondition.Click;
            }
            else if (strCondition == "time")
            {
                return XTutorialCmdFinishCondition.Time;
            }
            else if (strCondition == "TalkingNpc")
            {
                return XTutorialCmdFinishCondition.TalkingNpc;
            }
            else if (strCondition == "WorldMapShow")
            {
                return XTutorialCmdFinishCondition.WorldMap;
            }
            else if (strCondition == "SkillLevelup")
            {
                return XTutorialCmdFinishCondition.SkillLevelup;
            }
            else if (strCondition == "SkillBind")
            {
                return XTutorialCmdFinishCondition.SkillBind;
            }
            else if (strCondition == "UseItem")
            {
                return XTutorialCmdFinishCondition.UseItem;
            }
            else if (strCondition == "SysOpened")
            {
                return XTutorialCmdFinishCondition.SysOpened;
            }
            else if (strCondition == "GetReward")
            {
                return XTutorialCmdFinishCondition.GetReward;
            }
            else if (strCondition == "Move")
            {
                return XTutorialCmdFinishCondition.Move;
            }
            else if (strCondition == "ComposeFashion")
            {
                return XTutorialCmdFinishCondition.ComposeFashion;
            }
            else if (strCondition == "EnhanceItem")
            {
                return XTutorialCmdFinishCondition.EnhanceItem;
            }
            else if (strCondition == "ReinforceItem")
            {
                return XTutorialCmdFinishCondition.ReinforceItem;
            }
            else if (strCondition == "ChooseProf")
            {
                return XTutorialCmdFinishCondition.ChangeProf;
            }
            else if (strCondition == "HasTeam")
            {
                return XTutorialCmdFinishCondition.HasTeam;
            }
            else if (strCondition == "Smelting")
            {
                return XTutorialCmdFinishCondition.Smelting;
            }
            else if (strCondition == "SelectView")
            {
                return XTutorialCmdFinishCondition.SelectView;
            }
            else if (strCondition == "SelectSkipTutorial")
            {
                return XTutorialCmdFinishCondition.SelectSkipTutorial;
            }
            else
            {
                return XTutorialCmdFinishCondition.No_Condition;
            }
        }

        // Token: 0x040051FD RID: 20989
        private XTutorialCmd _parseTmpCmd;

        // Token: 0x040051FE RID: 20990
        private string[] _validateCmd = new string[]
        {
            "forceclick",
            "newsys",
            "exec",
            "forceslide",
            "noforceclick",
            "noforceslide",
            "forcedoubleclick",
            "puretext",
            "genericclick",
            "cutscene",
            "directsys",
            "movetutorial",
            "forceskill",
            "overlay",
            "showprefab",
            "notewindow",
            "hideskills",
            "showskills",
            "showbutton",
            "clickentity",
            "empty"
        };
    }
}
